#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;

int cnt[N],a[N],n,m,BL,tot,ans[N];
int l = 1,r;

struct query{
	int l,r,id,bl;
}q[N];

bool operator < (query a,query b){return a.bl ^ b.bl ? a.bl < b.bl : a.bl & 1 ? a.r < b.r : a.r > b.r;}

void add(int x){if(a[x] > n) return; tot -= cnt[a[x]] == a[x]; tot += ++cnt[a[x]] == a[x];}
void del(int x){if(a[x] > n) return; tot -= cnt[a[x]] == a[x]; tot += --cnt[a[x]] == a[x];}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in; BL = sqrt(n);
	for(int i = 1;i <= n;i++) a[i] = in;
	for(int i = 1;i <= m;i++){
		q[i].l = in,q[i].r = in,q[i].bl = (q[i].l - 1) / BL + 1;q[i].id = i;
	}
	sort(q+1,q+m+1);
	for(int i = 1;i <= m;i++){
		while(l > q[i].l) add(--l);
		while(l < q[i].l) del(l++);
		while(r > q[i].r) del(r--);
		while(r < q[i].r) add(++r);
		ans[q[i].id] = tot;
	}
	for(int i = 1;i <= m;i++) printf("%d\n",ans[i]);
	return 0;
}

